package com.java110.mall.shop.bmo.shopVisit.impl;

import com.java110.dto.shop.ShopVisitDto;
import com.java110.intf.shop.IShopVisitInnerServiceSMO;
import com.java110.mall.shop.bmo.shopVisit.IGetShopVisitBMO;
import com.java110.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service("getShopVisitBMOImpl")
public class GetShopVisitBMOImpl implements IGetShopVisitBMO {

    @Autowired
    private IShopVisitInnerServiceSMO shopVisitInnerServiceSMOImpl;

    /**
     * @param shopVisitDto
     * @return 订单服务能够接受的报文
     */
    public ResponseEntity<String> get(ShopVisitDto shopVisitDto) {


        int count = shopVisitInnerServiceSMOImpl.queryShopVisitsCount(shopVisitDto);

        List<ShopVisitDto> shopVisitDtos = null;
        if (count > 0) {
            shopVisitDtos = shopVisitInnerServiceSMOImpl.queryShopVisits(shopVisitDto);
            codeUserName(shopVisitDtos);
        } else {
            shopVisitDtos = new ArrayList<>();
        }

        ResultVo resultVo = new ResultVo((int) Math.ceil((double) count / (double) shopVisitDto.getRow()), count, shopVisitDtos);

        ResponseEntity<String> responseEntity = new ResponseEntity<String>(resultVo.toString(), HttpStatus.OK);

        return responseEntity;
    }

   private void codeUserName(List<ShopVisitDto> shopVisitDtos){
       for (ShopVisitDto shopVisitDto : shopVisitDtos ) {
            shopVisitDto.setVisitUserName("**"+shopVisitDto.getVisitUserName().substring(shopVisitDto.getVisitUserName().length()-1));
       }
   }
}
